<?php
include_once('./inc/config.php');
if(!is_logged_in()) redirect('index.php');	

$mode = $_POST['mode'];
$actions = array();
switch($mode)
{
	default:
		$actions['all_domains'] = get_all_domains();
 	break;

	case 'check':
		$actions = check_form();
		$actions['all_domains'] = get_all_domains();
	break;	
	
	case 'delete':
		$actions = delete_domains();
		$actions['all_domains'] = get_all_domains();
	break;
	
	case 'reset':
		$actions = reset_domains();
		$actions['all_domains'] = get_all_domains();
	break;
	
	
}


/** 
 * Check Form
 *
 * @descp Validate User in the DB
 * @access public
 * @param  NA	 
 * @return void
 */
function check_form()
{
	global $dbobj, $proj_config;

	$set = array();	
	$errors = '';	
	$d_name	= $_POST['d_name'];
	
	if(empty($d_name) || strlen($d_name) < 5)
	{
		$set['errors'] .= '<li>&raquo; Please enter a valid domain name (ex: www.abc.com)</li>';
	}
	else
	{
		if (!preg_match ($proj_config['domain_val'] , $d_name)) 
		{
			$set['errors'] .= '<li>&raquo; Please enter a valid domain name (ex: www.abc.com)</li>';
		}
	}
	
	if(empty($set['errors']))        
	{            		
	    $query = "SELECT d_name FROM ".TBL_DOMAINS." WHERE d_name = '".$dbobj->MySQLDB_Sanitize($d_name)."'";			
            // Perform Query
            $result = $dbobj->MySQLDB_GetData($query);		
            // Gwet num rows from the result set	
            $no_rows = $dbobj->MySQLDB_Numrows();					
            if($no_rows > 0)
            {		
                $set['errors'] .= '<li>&raquo; Domain name already exists in the system. Please add another domain</li>';	
            }		
			$dbobj->MySQLDB_Free($result);
            
            if(empty($set['errors']))        
            {
                $user_id = (isset($_SESSION['user_id'])) ? $_SESSION['user_id']:0;                
				$web_code = add_webcode($d_name,$user_id);                
				$set['web_code'] = $web_code;
				$set['d_name'] = $d_name;
                $set['congmsg'] = '<li>You have successfully added a new domain</li>';
            }
	}
	return $set;  
}


/** 
 * Get all domains
 *
 * @descp Get all domains based on the user id
 * @access public
 * @param  NA	 
 * @return void
 */
function get_all_domains()
{
	global $dbobj, $proj_config;
	
	$set = array();
	$query = "SELECT *
				FROM ".TBL_DOMAINS."
				WHERE 	user_id  = '".$_SESSION['user_id']."'	
				AND d_status = 1 
				";
		
	// Perform Query
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	if($no_rows > 0)
	{
		$set = $result;	
	}	
	$dbobj->MySQLDB_Free($result);	
	return $set;
}

/** 
 * Delete domains
 *
 * @descp this will delte the domina specified, but it basically just updates the status to 0
 * @access public
 * @param  NA	 
 * @return void
 */
function delete_domains()
{
	global $dbobj, $proj_config;
	$set = array();
	$domain_id = (int) $_POST['domain_id'];
	$query = "UPDATE ".TBL_DOMAINS."
				SET d_status = 0
				WHERE 	user_id  = '".$_SESSION['user_id']."'	
				AND d_id = '".$domain_id."'
				";
		
	// Perform Query
	$dbobj->MySQLDB_Query($query);
	$set['congmsg'] = '<li>Domain is successfully deleted from the system</li>';
	
	return $set;
}

/** 
 * Reset domains
 *
 * @descp this will delte the domina specified, but it basically just updates the status to 0
 * @access public
 * @param  NA	 
 * @return void
 */
function reset_domains()
{
	global $dbobj, $proj_config;
	$set = array();
	$domain_id = (int) $_POST['domain_id'];
	$query = "SELECT *
				FROM ".TBL_DOMAINS."
				WHERE 	user_id  = '".$_SESSION['user_id']."'	
				AND d_id = '".$domain_id."'
				AND c_code != ''
				";
		
	// Perform Query
	$result = $dbobj->MySQLDB_GetData($query);	
	// Gwet num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	if($no_rows > 0)
	{	
		// Use result
		$row = $result;
	
		/* Reset data from the following tabes 
		 * analytics_data
		 * total_time_on_page
		 */
		$c_code = $row['c_code']; 
		$query = "DELETE FROM ".TBL_ANA_DATA."
					WHERE an_code = '".$c_code."'";			
		// Perform Query
		$dbobj->MySQLDB_Query($query);
		
		$query = "DELETE FROM ".TBL_TIME_PG."
					WHERE an_code = '".$c_code."'";			
		// Perform Query
		$dbobj->MySQLDB_Query($query);
		
		$set['congmsg'] = '<li>Domain is successfully reset from the system</li>';
	}
	return $set;
}

/*************************************************************/
//Do not chnage this part
include(BASEPATH . 'views/header.php');//include header file

echo '<div class="bodytext">';//start of the body text
include(BASEPATH . 'views/dash_list.php');//include listing
include(BASEPATH . 'views/dash_quick_reg.php');//include quick register
echo '</div>';//end of the body text
include(BASEPATH . 'views/footer.php');//include footer file
/************************************************************/
?>